package com.gcd.tempe.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.gcd.tempe.LihatBarangActivity;

public class JSON_Response {

	
	public static String veryBaseUrl = "http://10.0.2.2:8080/Tempe/";
	public static String baseUrl = "http://10.0.2.2:8080/Tempe/service/?operation=";
	//public static String veryBaseUrl = "http://167.205.34.100:8080/Tempe/";
	//public static String baseUrl = "http://167.205.34.100:8080/Tempe/service/?operation=";
	
	private static String GET_ALL_BARANG = baseUrl + "barang.getAll";
	private static String GET_BARANG_BY_ID = baseUrl + "barang.get&barang_id=";
	private static String GET_BARANG_BY_TERM = baseUrl + "barang.getAll&term=";
	private static String GET_ALL_KATEGORI = baseUrl + "kategori.getAll";
	private static String GET_KATEGORI = baseUrl + "kategori.get&kategori_id=";
	private static String GET_BARANG_BY_KATEGORI = baseUrl + "barang.getAll&kategori_id=";
	private static String GET_USER_BY_USERNAME = baseUrl + "user.getInfo&username=";
	private static String GET_PROFIL = baseUrl + "profil.get";
	private static String GET_ALL_HISTORY = baseUrl + "pesanan.getAll&username=";	//[username]&password=[pass]&status=[nilainya 1, 2, 3 atau 4]
	private static String CONFIRM_PAYMENT = baseUrl + "pesanan.confirmPay&pesanan_id="; //[Id pesanan]&username=[username]&password=[pass]
	public static String DO_LOGIN = baseUrl + "login&username=";
	public static String GET_JASAPENGIRIMAN = baseUrl + "getShippingServices";
	
	//dummy j son responses
	public static String barangall = "[{'id':2,'kategori_id':2,'nama':'Kerudung','path_gambar':'upload/_Chrysanthemum.jpg','deskripsi':'Kerudung Cantik','harga_satuan':75000,'jumlah':0},{'id':3,'kategori_id':1,'nama':'Nastar','path_gambar':'upload/_Chrysanthemum.jpg','deskripsi':'Kue Nastar','harga_satuan':100000,'jumlah':0},{'id':4,'kategori_id':2,'nama':'Baju','path_gambar':'upload/not_defined.gif','deskripsi':'Baju','harga_satuan':50000,'jumlah':0},{'id':2,'kategori_id':2,'nama':'Kerudung','path_gambar':'upload/_Chrysanthemum.jpg','deskripsi':'Kerudung Cantik','harga_satuan':75000,'jumlah':0},{'id':3,'kategori_id':1,'nama':'Nastar','path_gambar':'upload/_Chrysanthemum.jpg','deskripsi':'Kue Nastar','harga_satuan':100000,'jumlah':0},{'id':4,'kategori_id':2,'nama':'Baju','path_gambar':'upload/not_defined.gif','deskripsi':'Baju','harga_satuan':50000,'jumlah':1}]";
	public static String kategori = "[{'id':1,'nama':'Kue','deskripsi':'Kue yang enak'},{'id':2,'nama':'Pakaian','deskripsi':'Pakaian Cantik'}]";
	public static String kategori1 = "[{'id':3,'kategori_id':1,'nama':'Nastar','path_gambar':'upload/_Chrysanthemum.jpg','deskripsi':'Kue Nastar','harga_satuan':100000,'jumlah':0}]";
	public static String kategori2 = "[{'id':2,'kategori_id':2,'nama':'Kerudung','path_gambar':'upload/_Chrysanthemum.jpg','deskripsi':'Kerudung Cantik','harga_satuan':75000,'jumlah':0},{'id':4,'kategori_id':2,'nama':'Baju','path_gambar':'upload/not_defined.gif','deskripsi':'Baju','harga_satuan':50000,'jumlah':0}]";
	
	public static JSONObject getItem(int id) {
		String serviceURL = GET_BARANG_BY_ID + id;
		String resultService = getJSONResponse(serviceURL);
		JSONObject jsonObject = stringToJSONObject(resultService);
		
		return jsonObject;
	}
	
	public static JSONArray getAllItems(){
		String serviceURL = GET_ALL_BARANG;
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public static JSONArray getItemsByTerm(String term) {
		String serviceURL = GET_BARANG_BY_TERM + term;
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public static JSONArray getItemsByTermAndCateg(String term, int id) {
		String serviceURL = GET_BARANG_BY_TERM + term + "&kategori_id=" + id;
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public static JSONArray getItemsByCategory(int categoryID) {
		String serviceURL = GET_BARANG_BY_KATEGORI + categoryID;
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public static JSONObject getCategory(int id) {
		String serviceURL = GET_KATEGORI + id;
		String resultService = getJSONResponse(serviceURL);
		JSONObject jsonObject = stringToJSONObject(resultService);
		
		return jsonObject;
	}
	
	public static JSONArray getAllCategories() {
		String serviceURL = GET_ALL_KATEGORI;
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public static JSONArray getAllHistory(String username, String password, int status){
		String serviceURL = GET_ALL_HISTORY + username + "&password=" + password; //[username]&password=[pass];
		if (status != 0){
			serviceURL += "&status=" + status;
		}
		System.out.println(serviceURL);
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		return jsonArray;
	}
	
	public static JSONArray getJasaPengiriman(){
		String serviceURL = GET_JASAPENGIRIMAN;
		String resultService = getJSONResponse(serviceURL);
		JSONArray jsonArray = stringToJSONArray(resultService);
		return jsonArray;
	}
	
	public static JSONObject getUser(String username) {
		String serviceURL = GET_USER_BY_USERNAME + username;
		String resultService = getJSONResponse(serviceURL);
		JSONObject jsonObject = stringToJSONObject(resultService);
		
		return jsonObject;
	}
	
	public static void orderItems(String username, String password, JSONArray jsonArrayItems) {
		
	}
	
	public static String confirmPayment(String username, String password, int orderID) {
		String serviceURL = CONFIRM_PAYMENT + orderID + "&username=" + username + "&password=" + password;
		String resultService = getJSONResponse(serviceURL);
		return resultService;
	}
	
	public static JSONObject getProfil() {
		String serviceURL = GET_PROFIL;
		String resultService = getJSONResponse(serviceURL);
		JSONObject jsonObject = stringToJSONObject(resultService);
		
		return jsonObject;
	}
	
	//primitive methods
	public static String getJSONResponse(String url) {
		
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				Log.e(LihatBarangActivity.class.toString(), "Failed to download file");
			}
		} catch (ClientProtocolException e) {
			Log.d("pret", "client protocol exception");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("pret", "io exception");
			e.printStackTrace();
		}
		return builder.toString();
	}
	
	
	public static JSONObject stringToJSONObject(String input) {
		JSONObject jsonObject = null;
		//Parse String to JSON Object
		try {
			jsonObject = new JSONObject(input);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	public static JSONArray stringToJSONArray(String input) {
		JSONArray jsonArray = null;
		//Parse String to JSON Array
		try {
			if (input != null && input != "") {
				jsonArray = new JSONArray(input);
			} else
				return jsonArray;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonArray;
	}
	
	public static Drawable ImageOperations(Context ctx, String url, String saveFilename) {
		try {
			Log.d("image", "masuk image operation");
			InputStream is = (InputStream)JSON_Response.fetch(JSON_Response.veryBaseUrl+url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}
}
